package com.example.demovote.controller;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

// 请求拦截器
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object loginedUser = session.getAttribute("loginedUser");
        if(loginedUser==null){
            String servletPath = request.getServletPath();
            if (request.getQueryString()!=null && request.getQueryString().isEmpty() == false) {
                servletPath += "?" + request.getQueryString();
            }
            System.out.println("servletPath = " + servletPath);
            session.setAttribute("servletPath", servletPath);
            response.sendRedirect("login.html?1");
            return false;
        }
        return true;
    }
}
